1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 import java.util.Arrays;
35 import javax.management.*;
36
37 public class InvokeGettersTest {
38 public static interface ThingMBean {
39 public int getWhatsit();
40 public void setWhatsit(int x);
41 public boolean isTrue();
42 }
43
44 public static class Thing implements ThingMBean {
45 public int getWhatsit() {
46 return whatsit;
47 }
48
49 public void setWhatsit(int x) {
50 whatsit = x;
51 }
52
53 public boolean isTrue() {
54 return true;
55 }
56
57 private int whatsit;
58 }
59
60 public static void main(String[] args) throws Exception {
61 MBeanServer mbs = MBeanServerFactory.newMBeanServer();
62 ObjectName on = new ObjectName("a:b=c");
63 mbs.registerMBean(new Thing(), on);
64 if (test(mbs, on, false))
65 System.out.println("OK: invoke without jmx.invoke.getters");
66 System.setProperty("jmx.invoke.getters", "true");
67 if (test(mbs, on, true))
68 System.out.println("OK: invoke with jmx.invoke.getters");
69 if (failure == null)
70 System.out.println("TEST PASSED");
71 else
72 throw new Exception("TEST FAILED: " + failure);
73 }
74
75 private static boolean test(MBeanServer mbs, ObjectName on,
76 boolean shouldWork) throws Exception {
77 ++x;
78 mbs.setAttribute(on, new Attribute("Whatsit", x));
79 Integer got = (Integer) mbs.getAttribute(on, "Whatsit");
80 if (got != x)
81 return fail("Set attribute was not got: " + got);
82 ++x;
83 try {
84 mbs.invoke(on, "setWhatsit", new Object[] {x}, new String[] {"int"});
85 if (!shouldWork)
86 return fail("invoke setWhatsit worked but should not have");
87 System.out.println("invoke setWhatsit worked as expected");
88 } catch (ReflectionException e) {
89 if (shouldWork)
90 return fail("invoke setWhatsit did not work but should have");
91 System.out.println("set got expected exception: " + e);
92 }
93 try {
94 got = (Integer) mbs.invoke(on, "getWhatsit", null, null);
95 if (!shouldWork)
96 return fail("invoke getWhatsit worked but should not have");
97 if (got != x)
98 return fail("Set attribute through invoke was not got: " + got);
99 System.out.println("invoke getWhatsit worked as expected");
100 } catch (ReflectionException e) {
101 if (shouldWork)
102 return fail("invoke getWhatsit did not work but should have");
103 System.out.println("get got expected exception: " + e);
104 }
105 try {
106 boolean t = (Boolean) mbs.invoke(on, "isTrue", null, null);
107 if (!shouldWork)
108 return fail("invoke isTrue worked but should not have");
109 if (!t)
110 return fail("isTrue returned false");
111 System.out.println("invoke isTrue worked as expected");
112 } catch (ReflectionException e) {
113 if (shouldWork)
114 return fail("invoke isTrue did not work but should have");
115 else
116 System.out.println("isTrue got expected exception: " + e);
117 }
118
119
120 final Object[][] badInvokes = {
121 {"isWhatsit", null, null},
122 {"getWhatsit", new Object[] {5}, new String[] {"int"}},
123 {"setWhatsit", new Object[] {false}, new String[] {"boolean"}},
124 {"getTrue", null, null},
125 };
126 boolean ok = true;
127 for (Object[] args : badInvokes) {
128 String name = (String) args[0];
129 Object[] params = (Object[]) args[1];
130 String[] sig = (String[]) args[2];
131 String what =
132 "invoke " + name + (sig == null ? "[]" : Arrays.toString(sig));
133 try {
134 mbs.invoke(on, name, params, sig);
135 ok = fail(what + " worked but should not have");
136 } catch (ReflectionException e) {
137 if (e.getCause() instanceof NoSuchMethodException)
138 System.out.println(what + " failed as expected");
139 else {
140 ok = fail(what + " got exception with wrong nested " +
141 "exception: " + e.getCause());
142 }
143 }
144 }
145
146 return ok;
147 }
148
149 private static boolean fail(String why) {
150 failure = why;
151 System.out.println("FAILED: " + why);
152 return false;
153 }
154
155 private static int x;
156 private static String failure;
157 }